#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import os
import sys

# 添加项目根目录到路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def add_update_cases_to_frameworks():
    """为 test_frameworks.json 添加更新操作的测试用例"""
    frameworks_file = "data/test_frameworks.json"
    
    if not os.path.exists(frameworks_file):
        return False
    
    # 读取现有数据
    with open(frameworks_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 为每个对象添加更新用例
    for object_name, object_data in data.items():
        frameworks = object_data.get("frameworks", [])
        update_frameworks = []
        
        for framework in frameworks:
            # 创建更新版本的框架
            update_framework = framework.copy()
            update_framework["case_type"] = "update"
            
            # 更新用例名称和描述
            if "创建" in update_framework["case_name"]:
                update_framework["case_name"] = update_framework["case_name"].replace("创建", "更新")
            if "创建" in update_framework["description"]:
                update_framework["description"] = update_framework["description"].replace("创建", "更新")
            if "创建成功" in update_framework["expected_result"]:
                update_framework["expected_result"] = update_framework["expected_result"].replace("创建成功", "更新成功")
            if "创建失败" in update_framework["expected_result"]:
                update_framework["expected_result"] = update_framework["expected_result"].replace("创建失败", "更新失败")
            
            update_frameworks.append(update_framework)
        
        # 将更新用例添加到现有用例后面
        frameworks.extend(update_frameworks)
    
    # 保存更新后的数据
    with open(frameworks_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    
    return True

def add_update_cases_to_test_cases():
    """为 test_cases.json 添加更新操作的测试用例"""
    test_cases_file = "data/test_cases.json"
    
    if not os.path.exists(test_cases_file):
        return False
    
    # 读取现有数据
    with open(test_cases_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 为每个对象添加更新用例
    for object_name, object_data in data.items():
        test_cases = object_data.get("test_cases", [])
        update_test_cases = []
        
        for test_case in test_cases:
            # 创建更新版本的测试用例
            update_test_case = test_case.copy()
            update_test_case["case_type"] = "update"
            
            # 更新用例名称和描述
            if "创建" in update_test_case["case_name"]:
                update_test_case["case_name"] = update_test_case["case_name"].replace("创建", "更新")
            if "创建" in update_test_case["description"]:
                update_test_case["description"] = update_test_case["description"].replace("创建", "更新")
            if "创建成功" in update_test_case["expected_result"]:
                update_test_case["expected_result"] = update_test_case["expected_result"].replace("创建成功", "更新成功")
            if "创建失败" in update_test_case["expected_result"]:
                update_test_case["expected_result"] = update_test_case["expected_result"].replace("创建失败", "更新失败")
            
            update_test_cases.append(update_test_case)
        
        # 将更新用例添加到现有用例后面
        test_cases.extend(update_test_cases)
    
    # 保存更新后的数据
    with open(test_cases_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    
    return True

def add_delete_cases_to_frameworks():
    """为 test_frameworks.json 添加删除操作的测试用例"""
    frameworks_file = "data/test_frameworks.json"
    
    if not os.path.exists(frameworks_file):
        return False
    
    # 读取现有数据
    with open(frameworks_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 为每个对象添加删除用例
    for object_name, object_data in data.items():
        frameworks = object_data.get("frameworks", [])
        
        # 添加删除和查询用例
        additional_frameworks = [
            {
                "case_name": "删除记录-正常删除",
                "case_type": "delete",
                "scenario": "normal",
                "description": "正常删除记录",
                "test_data_type": "valid",
                "target_field": "",
                "expected_result": "删除成功，返回删除的记录ID"
            },
            {
                "case_name": "删除记录-删除不存在的记录",
                "case_type": "delete",
                "scenario": "exception",
                "description": "尝试删除不存在的记录",
                "test_data_type": "invalid",
                "target_field": "",
                "expected_result": "删除失败，记录不存在"
            },
            {
                "case_name": "查询记录-按ID查询",
                "case_type": "read",
                "scenario": "normal",
                "description": "按记录ID查询记录",
                "test_data_type": "valid",
                "target_field": "",
                "expected_result": "查询成功，返回记录详情"
            },
            {
                "case_name": "查询记录-按唯一字段查询",
                "case_type": "read",
                "scenario": "normal",
                "description": "按唯一字段查询记录",
                "test_data_type": "valid",
                "target_field": "",
                "expected_result": "查询成功，返回记录详情"
            },
            {
                "case_name": "查询记录-分页查询",
                "case_type": "read",
                "scenario": "normal",
                "description": "按分页查询记录",
                "test_data_type": "valid",
                "target_field": "",
                "expected_result": "查询成功，返回分页记录列表"
            }
        ]
        
        # 将删除和查询用例添加到现有用例后面
        frameworks.extend(additional_frameworks)
    
    # 保存更新后的数据
    with open(frameworks_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    
    return True

def add_delete_cases_to_test_cases():
    """为 test_cases.json 添加删除操作的测试用例"""
    test_cases_file = "data/test_cases.json"
    
    if not os.path.exists(test_cases_file):
        return False
    
    # 读取现有数据
    with open(test_cases_file, 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    # 为每个对象添加删除用例
    for object_name, object_data in data.items():
        test_cases = object_data.get("test_cases", [])
        
        # 添加删除和查询用例
        additional_test_cases = [
            {
                "case_name": "删除记录-正常删除",
                "case_type": "delete",
                "scenario": "normal",
                "description": "正常删除记录",
                "test_data": {
                    "_id": 1
                },
                "expected_result": "删除成功，返回删除的记录ID"
            },
            {
                "case_name": "删除记录-删除不存在的记录",
                "case_type": "delete",
                "scenario": "exception",
                "description": "尝试删除不存在的记录",
                "test_data": {
                    "_id": 999999999999
                },
                "expected_result": "删除失败，记录不存在"
            },
            {
                "case_name": "查询记录-按ID查询",
                "case_type": "read",
                "scenario": "normal",
                "description": "按记录ID查询记录",
                "test_data": {
                    "_id": 1
                },
                "expected_result": "查询成功，返回记录详情"
            },
            {
                "case_name": "查询记录-按唯一字段查询",
                "case_type": "read",
                "scenario": "normal",
                "description": "按唯一字段查询记录",
                "test_data": {
                    "_id": 1
                },
                "expected_result": "查询成功，返回记录详情"
            },
            {
                "case_name": "查询记录-分页查询",
                "case_type": "read",
                "scenario": "normal",
                "description": "按分页查询记录",
                "test_data": {
                    "page": 1,
                    "size": 10
                },
                "expected_result": "查询成功，返回分页记录列表"
            }
        ]
        
        # 将删除和查询用例添加到现有用例后面
        test_cases.extend(additional_test_cases)
    
    # 保存更新后的数据
    with open(test_cases_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    
    return True

def main():
    """主函数"""
    # 添加更新用例到框架文件
    success1 = add_update_cases_to_frameworks()
    
    # 添加更新用例到测试用例文件
    success2 = add_update_cases_to_test_cases()
    
    # 添加删除用例到框架文件
    success3 = add_delete_cases_to_frameworks()
    
    # 添加删除用例到测试用例文件
    success4 = add_delete_cases_to_test_cases()
    
    if success1 and success2 and success3 and success4:
        pass  # 静默成功
    else:
        pass  # 静默失败

if __name__ == "__main__":
    main() 